package com.vedana.bankmobile.client;

import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.vedana.bankmobile.client.pager.FixedPager;
import com.vedana.bankmobile.shared.Operation;

public class OperationsList extends Composite {

	private static OperationsListUiBinder uiBinder = GWT
			.create(OperationsListUiBinder.class);

	@UiField(provided = true) CellTable<Operation> table;
	
	@UiField(provided=true) FixedPager pager;
	
	private final BankMobileRpcServiceAsync dataService = GWT.create(BankMobileRpcService.class);


	interface OperationsListUiBinder extends UiBinder<Widget, OperationsList> {
	}

	/**
	 * Constructor
	 */
	public OperationsList() {
		// Initialize the table widget.
		createTable();

		initWidget(uiBinder.createAndBindUi(this));

		// Create a data provider.
		AsyncDataProvider<Operation> dataProvider = new AsyncDataProvider<Operation>() {
			@Override
			protected void onRangeChanged(HasData<Operation> display) {
				final int start = display.getVisibleRange().getStart();
				final int length = display.getVisibleRange().getLength();

				AsyncCallback<List<Operation>> callback = new AsyncCallback<List<Operation>>() {
					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}

					@Override
					public void onSuccess(List<Operation> result) {
						if (result.size() < length) {
							// We've reached the end
							table.setRowCount(start + result.size());
						}
						updateRowData(start, result);
					}
				};
				dataService.getRangeOfOperations(123456789, start, length, callback);
			}
		};
		dataProvider.addDataDisplay(table);
	}

	/**
	 * Create the {@link CellTable}.
	 */
	private void createTable() {
		// Page size = 20
		table = new CellTable<Operation>(20);
		table.setWidth("100%");
		final DateTimeFormat dtf = DateTimeFormat.getFormat("dd/MM/yyyy");
		// Date column
		Column<Operation, Date> dateCol = new Column<Operation, Date>(
				new DateCell(dtf)) {
			@Override
			public Date getValue(Operation oper) {
				return oper.getDate();
			}
		};
		table.addColumn(dateCol, "Date");

		// Label column
		Column<Operation, String> labelCol = new Column<Operation, String>(
				new TextCell()) {
			@Override
			public String getValue(Operation oper) {
				return oper.getLabel();
			}
		};
		table.addColumn(labelCol, "Label");

		// Value column
		final NumberFormat nf = NumberFormat.getFormat(".00 \u00A4", "EUR");
		Column<Operation, Number> valueCol = new Column<Operation, Number>(
				new NumberCell(nf)) {
			@Override
			public Number getValue(Operation oper) {
				return oper.getValue();
			}
		};
		table.addColumn(valueCol, "Value");
		
		// create pager
		pager = new FixedPager();
		pager.setDisplay(table);
		pager.setRangeLimited(true);
	}
}
